<template>
    <div class="goods-list">
        <div v-for="item in goodsList" :key="item.id" @click="redirect(item.id)" class="goods-item">
            <img :src="item.img_url">
            <h2 class="title">{{item.title}}</h2>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}&nbsp;&nbsp;</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        <mt-button type="danger" size="large" v-if="moreBtn" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import connector from '../../base/interface.js';
    export default {
        data(){
            return{
                pageindex:1,
                goodsList:[],
                /*控制加载更多按钮显示*/
                moreBtn:true
            }
        },methods:{
            getGoods(){
                /*获取商品列表数据*/
                connector.getGoods(this.pageindex).then(result=>{
                    /*拼接新数组*/
                    if(!result.length){
                        this.moreBtn=false;
                    }
                    this.goodsList.push(...result);
                },err=>{
                    console.log(err.message);
                });
            }, getMore(){
                this.pageindex++;
                this.getGoods();
            },redirect(id){
                /*编程式导航*/
                this.$router.push({name:'goodsinfo',params:{id}});
            }
        },created() {
            this.getGoods();
        }
    }
</script>

<style  lang="scss" scoped>
@import "../../base/baseRem.scss";
.goods-list{
    display: flex;
    flex-wrap:wrap;
    padding:4px;
    justify-content: space-between;
    .goods-item{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 3px 1px;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: remFu(250);
        .info{
            background-color: #ddd;
            p{margin: 0}
        }
        .title{
            font-size: remFu(12);
            line-height: remFu(18);
            padding: 0 2px;
            overflow: hidden;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            word-break: break-all;
        }
        .price{
            padding: 0 5px;
            .now{
                color: red;
                font-size: remFu(16);
                font-weight: bold;
            }
            .old{
                text-decoration:line-through
            }
        }
        .sell{
            font-size: remFu(10);
            display: flex;
            justify-content: space-between;
            padding: 0 5px;
        }
    }
}
</style>
